package com.iplant.ams.controller.dms.maintenance;

import com.iplant.ams.service.DRMService;
import com.iplant.ams.service.po.dms.maintenance.DMSConfiguration;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 设备构型控制器
 */
@RestController
@RequestMapping("/api/DMSConfiguration")
public class DMSConfigurationController extends BasicController<DMSConfiguration> {
    private static Logger logger = LoggerFactory.getLogger(DMSConfigurationController.class);


    public DMSConfigurationController() {
        super(DMSConfiguration.class);
    }

    @Autowired
    DRMService mDRMService;

    /**
     * 保存设备构型（支持同步保存构型BOM）
     */
    @Override
    @PostMapping("/Update")
    public Object Update(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            DMSConfiguration wDMSConfiguration = CloneTool.Clone(wParam.get("data"), DMSConfiguration.class);

            ServiceResult<Integer> wServiceResult = mDRMService.DMS_UpdateConfiguration(wLoginUser, wDMSConfiguration);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 初始化设备构型
     */
    @GetMapping("/InitConfiguration")
    public Object InitConfiguration(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            ServiceResult<Integer> wServiceResult = mDRMService.DRM_InitConfiguration(wLoginUser);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }
}
